////////////////////////////////////////////////////////////////////////////////
// This file is part of the Mist compiler.                                    //
//                                                                            //
// Copyright (C) 2008, 2009 The Mist project                                  //
//                                                                            //
// For a list of contributors, see the COMMITTERS file.                       //
//                                                                            //
// The Mist compiler is free software: you can redistribute it and/or modify  //
// it under the terms of the GNU General Public License as published by       //
// the Free Software Foundation, either version 3 of the License, or          //
// (at your option) any later version.                                        //
//                                                                            //
// The Mist compiler is distributed in the hope that it will be useful,       //
// but WITHOUT ANY WARRANTY; without even the implied warranty of             //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              //
// GNU General Public License for more details.                               //
//                                                                            //
// You should have received a copy of the GNU General Public License          //
// in the COPYING file. If not, see <http://www.gnu.org/licenses/>.           //
////////////////////////////////////////////////////////////////////////////////

///
/// \file
/// The common/string.hpp test-suite.
///

////////////////////////////////////////////////////////////////////////////////
// Includes                                                                   //
////////////////////////////////////////////////////////////////////////////////

#include "common/string.hpp"
#include "test/common.hpp"

////////////////////////////////////////////////////////////////////////////////
// Test Cases                                                                 //
////////////////////////////////////////////////////////////////////////////////

BOOST_AUTO_TEST_SUITE( suite_common_string )
	
	BOOST_AUTO_TEST_CASE( test_to_string ) {
		BOOST_CHECK_EQUAL( to_string(100  ), "100"    );
		BOOST_CHECK_EQUAL( to_string(-50  ), "-50"    );
		BOOST_CHECK_EQUAL( to_string(3e5  ), "300000" );
		BOOST_CHECK_EQUAL( to_string(0.1  ), "0.1"    );
		BOOST_CHECK_EQUAL( to_string(-0.1 ), "-0.1"   );
		BOOST_CHECK_EQUAL( to_string(0.1e5), "10000"  );
		BOOST_CHECK_EQUAL( to_string("foo"), "foo"    );
	}
	
	BOOST_AUTO_TEST_CASE( test_to ) {
		BOOST_CHECK_EQUAL( to<int>   ("100"  ),  100     );
		BOOST_CHECK_EQUAL( to<int>   ("-50"  ), -50      );
		BOOST_CHECK_EQUAL( to<double>("0.1"  ),  0.1     );
		BOOST_CHECK_EQUAL( to<double>("-0.1" ), -0.1     );
		BOOST_CHECK_EQUAL( to<double>("0.1e5"),  10000.0 );
		BOOST_CHECK_EQUAL( to<double>("100")  ,  100     );
		
		BOOST_CHECK_THROW( to<int>   ("3e5"  ), AssertionFailure );
		BOOST_CHECK_THROW( to<int>   ("5.4"  ), AssertionFailure );
		BOOST_CHECK_THROW( to<int>   ("text" ), AssertionFailure );
		BOOST_CHECK_THROW( to<double>("text" ), AssertionFailure );
	}
	
	BOOST_AUTO_TEST_CASE( test_is ) {
		BOOST_CHECK_PREDICATE( is<int>        , ("100"  ) );
		BOOST_CHECK_PREDICATE( is<int>        , ("-50"  ) );
		BOOST_CHECK_PREDICATE( is<double>     , ("0.1"  ) );
		BOOST_CHECK_PREDICATE( is<double>     , ("-0.1" ) );
		BOOST_CHECK_PREDICATE( is<double>     , ("0.1e5") );
		BOOST_CHECK_PREDICATE( is<double>     , ("100"  ) );
		
		BOOST_CHECK_PREDICATE( neg(is<int>)   , ("3e5"  ) );
		BOOST_CHECK_PREDICATE( neg(is<int>)   , ("5.4"  ) );
		BOOST_CHECK_PREDICATE( neg(is<int>)   , ("text" ) );
		BOOST_CHECK_PREDICATE( neg(is<double>), ("text" ) );
	}
	
	BOOST_AUTO_TEST_CASE( test_implode ) {
		vector<string> s;
		s += "1","2","3","4","5";
		
		BOOST_CHECK_EQUAL( implode(s, ","          ), "1,2,3,4,5"        );
		BOOST_CHECK_EQUAL( implode(s, ", ", " and "), "1, 2, 3, 4 and 5" );
		
		vector<int> v;
		v += 1,2,3,4,5;
		
		BOOST_CHECK_EQUAL( implode(v, ","          ), "1,2,3,4,5"        );
		BOOST_CHECK_EQUAL( implode(v, ", ", " and "), "1, 2, 3, 4 and 5" );
	}
	
BOOST_AUTO_TEST_SUITE_END()
